# -*- coding: UTF-8 -*-

from .BaseController import BaseController

class UserLoginIn(BaseController):

    def execute(self, saveCookies = True, cmdQrcode = True):
        fetchLoginInfoRes = self._fetchUserLoginInfo(saveCookies =  False, cmdQrcode = cmdQrcode)

        if not fetchLoginInfoRes.success:
            self.core.lib.stdout(fetchLoginInfoRes.message)

        uin = fetchLoginInfoRes.data
        syncUserinfoRes = self._syncUserinfo(uin)
        if not syncUserinfoRes.success:
            self.core.lib.stdout(syncUserinfoRes.message)

        handleRes = self._actionListener(uin)
        self.core.lib.stdout(handleRes.message)


    def _fetchUserLoginInfo(self, saveCookies = True, cmdQrcode = True):
        uuid = ''
        wechatUserLoginInService = self.core.loader.service('WechatUserLoginInService')

        if saveCookies:
            loadStateRes = wechatUserLoginInService.loadLoginState()
            if loadStateRes.success:
                needScanQrcodeRes = wechatUserLoginInService.isNeedScanQrcode()
                if needScanQrcodeRes.success:
                    uuid = needScanQrcodeRes.data

        if not uuid:
            uuid = wechatUserLoginInService.getQRuuid()
            if not uuid:
                return self.core.lib.outputobj(False, u'uuid获取失败')
            wechatUserLoginInService.getQRcode(uuid, cmdQrcode)
       
        prepareRes = wechatUserLoginInService.prepareLoginInfo(uuid)
        if not prepareRes.success:
            return self.core.lib.outputobj(False, prepareRes.message)

        if saveCookies:
            res = wechatUserLoginInService.saveLoginState()

        return prepareRes


    def _syncUserinfo(self, uin):
        wechatUserSyncAuthService = self.core.loader.service('WechatUserSyncAuthService')
        syncUserinfoRes = wechatUserSyncAuthService.fetchUserinfoDetail(uin)

        if not syncUserinfoRes.success:
            return self.core.lib.outputobj(False, syncUserinfoRes.message)

        handleInfoRes = wechatUserSyncAuthService.handleUserinfoContent(uin, syncUserinfoRes.data)
        if not handleInfoRes.success:
            return self.core.lib.outputobj(False, handleInfoRes.message)

        checkNotifyStatusRes = wechatUserSyncAuthService.checkNotifyStatus(uin)
        if checkNotifyStatusRes.success:
            return self.core.lib.outputobj(True, '信息同步检测通过')

        if checkNotifyStatusRes.errcode == 1101:
            execute(self, False)

        return checkNotifyStatusRes


    def _actionListener(self, uin):
        try:
            wechatUserOnlineActionHandlerService = self.core.loader.service('WechatUserOnlineActionHandlerService')
            mThreading = self.core.loader.module('threading')
            mainThread = mThreading.Thread(target = wechatUserOnlineActionHandlerService.actionHandler, args = (uin,), name = 'wechat_marvin_robot')
            mainThread.run()
            return self.core.lib.outputobj(True, '后台进程已退出')
        except Exception as e:
            return self.core.lib.outputobj(False, '进程出错：' . str(e))
        finally:
            wechatUserLoginOutService = self.core.loader.service('WechatUserLoginOutService')
            wechatUserLoginOutService.logout(uin)

